package com.api.rocky.base;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.api.rocky.R;
import com.api.rocky.Util.ImageLoader;
import com.api.rocky.model.MainMsgList;

public class MessageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    private boolean mBusy = false;
    private ImageLoader mImageLoader;
      
    public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}
      
    public MessageAdapter(Context context,List<Map<String, Object>> mData){ 
        this.mInflater = LayoutInflater.from(context);
        this.mData = mData;
        mImageLoader = new ImageLoader();
    } 
    public int getCount() { 
        return mData.size(); 
    } 

    public Object getItem(int arg0) { 
        return mData.get(arg0); 
    } 

    public long getItemId(int arg0) { 
        return arg0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
          
        MainMsgList holder = null; 
        if (convertView == null) { 
              
            holder=new MainMsgList();   
              
            convertView = mInflater.inflate(R.layout.listview_main_msg, null); 
            holder.face = (ImageView)convertView.findViewById(R.id.face); 
            holder.name = (TextView)convertView.findViewById(R.id.name); 
            holder.time = (TextView)convertView.findViewById(R.id.time); 
            holder.message = (TextView)convertView.findViewById(R.id.msg);
            convertView.setTag(holder); 
              
        }else { 
              
            holder = (MainMsgList)convertView.getTag(); 
        } 
        if (!mBusy) {
			mImageLoader.loadImage((String)mData.get(position).get("face"), this, holder);
		} else {
			Bitmap bitmap = mImageLoader.getBitmapFromCache((String)mData.get(position).get("face"));
			if (bitmap != null) {
				holder.face.setImageBitmap(bitmap);
			} else {
				holder.face.setImageResource(R.drawable.ic_launcher);
			}
		}  
          
        holder.name.setText((String)mData.get(position).get("name")); 
        holder.time.setText((String)mData.get(position).get("time")); 
        holder.message.setText((String)mData.get(position).get("msg")); 
        return convertView; 
    }
  
}
